<?php

class Notification extends CApplicationComponent
{
    protected $class='', $message='';

    public function getClass()
    {
        if(!$this->class){
            $this->class=Yii::app()->session['notificationClass'];
        }

        return $this->class;
    }

    public function getMessage()
    {
        if(!$this->class){
            $message=Yii::app()->session['notificationMessage'];
            Yii::app()->session['notificationMessage']='';
        }else{
            $message=$this->message;
            $this->message='';
        }

        return $message;
    }

    public function setMessage($message)
    {
        $this->class='message';
        if(is_array($message) || is_object($message)){
            foreach($message as $mes){
                $this->setMessage($mes);
            }
        }
        $this->message.=$message.' /n';
        Yii::app()->session['notificationClass']=$this->class;
        Yii::app()->session['notificationMessage']=$this->message;
    }

    public function setWarning($message)
    {
        $this->class='warning';
        if(is_array($message) || is_object($message)){
            foreach($message as $mes){
                $this->setWarning($mes);
            }
        }
        $this->message.=$message.' /n';
        Yii::app()->session['notificationClass']=$this->class;
        Yii::app()->session['notificationMessage']=$this->message;
    }

    public function setError($message)
    {
        $this->class='error';
        if(is_array($message) || is_object($message)){
            foreach($message as $mes){
                $this->setError($mes);
            }
        }

        $this->message.=$message.' /n';
        Yii::app()->session['notificationClass']=$this->class;
        Yii::app()->session['notificationMessage']=$this->message;
    }

    public function getEchoMessage()
    {
        if($message=$this->getMessage()){
            $message=str_replace('/n','',$message);
            Yii::app()->clientScript->registerScript('echo-message', $this->getScript($message, $this->getClass()));
        }
    }

    protected function getScript($message,$class='')
    {
        return '
                var notify=$("#notification-message");
                notify.css("display", "block");
                notify.addClass("'.$class.'");
                notify.html("'.$message.'");
            ';
    }
}